/**
 * http://localhost:8083/proj 取得项目根路径
 */
function getRootPath() {
	// 获取当前网址，如： http://localhost:8083/proj/meun.jsp
	var curWwwPath = window.document.location.href;
	// 获取主机地址之后的目录，如： proj/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8083
	var localhostPath = curWwwPath.substring(0, pos);
	// 获取带"/"的项目名，如：/proj
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPath + projectName);
}

/**
 * 查询商品分类列表数据
 */
$(function() {
	var currentPage = 1;
	ptlist(currentPage);
});

function ptlist(currentPage) {
	$
			.ajax({
				type : 'post',
				url : getRootPath() + "/product/productList.do",
				data : {
					"currentPage" : currentPage,
					"memberId" : 'member001'
				},
				async : 'true',
				dataType : 'json',
				success : function(data) {
					var rec = data.page;
					// alert(rec.list[1].productTypeName);
					var html = "";
					for (i = 0; i < rec.list.length; i++) {
						html += "<tr>";
						html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="
								+ rec.list[i].id + " ></td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].id + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].productId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].productName + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].unit + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].stock + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].sales + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].productRemarks + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].putawayDt + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].soldoutDt + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].status + "</td>";
						html += "</tr>";
					}
					$("#data_list").html(html);
					// 鼠标经过td的时候就显示td内容
					jQuery(function() {
						// 使用class选择器;例如:list对象->tbody->td对象.
						$("#data_list td").each(function(i) {
							// 给td设置title属性,并且设置td的完整值.给title属性.
							$(this).attr("title", $(this).text());

						});
					});
					var pager = data.page;
					$(".pag_in").html(
							G.getPager(pager.totalRecords, pager.currentPage,
									10, 10, 'ptlist'));
				}

			});
}

/**
 * 模糊查找
 */
function searchpro(currentPage) {
	var keyWord = $("#searchForm :input[id=keyword]").val();
	var status = $("#s_status").find("option:selected").val();
	var upstarttime = $("#searchForm :input[id=upstarttime]").val();
	var upendtime = $("#searchForm :input[id=upendtime]").val();
	var downstarttime = $("#searchForm :input[id=downstarttime]").val();
	var downendtime = $("#searchForm :input[id=downendtime]").val();

	$.ajax({
				type : 'post',
				url : getRootPath() + "/product/productList.do",
				data : {
					"currentPage" : currentPage,
					"memberId" : 'member001',
					"keyWord" : keyWord,
					"upstarttime" : upstarttime,
					"upendtime" : upendtime,
					"downendtime" : downendtime,
					"downstarttime" : downstarttime,
					"status" : status
				},
				async : 'true',
				dataType : 'json',
				success : function(data) {
					var rec = data.page;
					// alert(rec.list[1].productTypeName);
					var html = "";
					for (i = 0; i < rec.list.length; i++) {
						html += "<tr>";
						html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="
								+ rec.list[i].id + " ></td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].id + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].productId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].productName + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].unit + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].stock + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].sales + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].productRemarks + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].putawayDt + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].soldoutDt + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].status + "</td>";
						html += "</tr>";
					}
					$("#data_list").html(html);
					// 鼠标经过td的时候就显示td内容
					jQuery(function() {
						// 使用class选择器;例如:list对象->tbody->td对象.
						$("#data_list td").each(function(i) {
							// 给td设置title属性,并且设置td的完整值.给title属性.
							$(this).attr("title", $(this).text());

						});
					});
					var pager = data.page;
					$(".pag_in").html(
							G.getPager(pager.totalRecords, pager.currentPage,
									10, 10, 'ptlist'));
				}

			});
}

function insert() {
	$.ajax({
		type : 'post',
		url : getRootPath() + "/product/insertPRO.do",
		data : $('#productform').serialize(),
		async : 'false',
		dataType : 'json',
		success : function(data) {
			if (data.resultMsg != 1) {
				layer.msg('失败', {
					icon : 2
				});
			} else {
				layer.msg('成功', {
					icon : 1
				});
				$("#myModal_1").modal("hide");
				ptlist(1);
			}
		}
	});
}

function search1(currentPage) {
	var userLevelId = $("#searchForm select[name=userLevelId]").val();
	var stauts = $("#searchForm select[name=status]").val();
	var keyWord = $("#searchForm input[name=keyWord]").val();
	$
			.ajax({
				type : 'post',
				url : getRootPath() + "/creditsT/ctList.do",
				data : {
					"currentPage" : currentPage,
					"userLevelId" : userLevelId,
					"stauts" : status,
					"keyWord" : keyWord
				},
				async : 'true',
				dataType : 'json',
				success : function(data) {
					var rec = data.page;
					// alert(rec.list[1].productTypeName);
					var html = "";
					for (i = 0; i < rec.list.length; i++) {
						html += "<tr>";
						html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="
								+ rec.list[i].id + " ></td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].id + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsTypeId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].memberId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsTypeName + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].userLevelId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsTypeContent + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].status + "</td>";
						html += "</tr>";
					}
					$("#list_1").html(html);
					var pager = data.page;
					$(".pag_in").html(
							G.getPager(pager.totalRecords, pager.currentPage,
									10, 10, 'search'));
				}
			});
}

function getPRO() {
	var chk_value = [];
	var len = $('#list_1 :input[name="ck-all"]').length;
	var id;
	var num = 0;
	$('input[name="ck-all"]:checked').each(function() {
		num++;
		id = $(this).val();
	});
	if (num > 1) {
		alert("每次只能选择一条记录修改@@@");
	} else {
		getPRO_record(id);
	}
}

function delPRO() {
	var chk_value = [];
	var len = $('#list_1 :input[name="ck-all"]').length;
	var ids;
	$('input[name="ck-all"]:checked').each(function() {
		ids += $(this).val() + ",";
	});
	delPRO_record(ids);
}

function delPRO_record(ids) {
	$.ajax({
		type : 'post',
		url : getRootPath() + "/product/delPRO.do",
		data : {
			"ids" : ids
		},
		dataType : 'json',
		success : function(data) {
			if (data.resultMsg != 1) {
				layer.msg('失败', {
					icon : 2
				});
			} else {
				layer.msg('成功', {
					icon : 1
				});
				ptlist(1);
			}
		}
	});
}

function getPRO_record(id) {
	$.ajax({
		type : 'post',
		url : getRootPath() + "/product/getPRO.do",
		data : {
			"id" : id
		},
		/* async : 'true', */
		dataType : 'json',
		success : function(data) {
			rec = eval(data.record);
			$("#productform :input[name=productId]").val(rec.productId);
			$("#productform :input[name=productName]").val(rec.productName);
			$("#productform :input[name=productRemarks]").val(
					rec.productRemarks);
			$("#productform :input[name=unit]").val(rec.unit);
			$("#productform :input[name=stock]").val(rec.stock);
			$("#productform :input[name=putawayDt]").val(rec.putawayDt);
			$("#productform :input[name=soldoutDt]").val(rec.soldoutDt);
			$("#productform :input[name=remarks]").val(rec.remarks);
			$("#productform :radio[name=status]").val(rec.status);
		}
	});
}

// jquery获取复选框值
function jqchk() {
	var chk_value = [];
	var len = $('#list_1 :input[name="ck-all"]').length;
	var creditsTypeId;
	var num = 0;
	$('input[name="ck-all"]:checked').each(function() {
		num++;
		creditsTypeId = $(this).val();
	});
	if (num > 1) {
		alert("每次只能选择一条记录修改@@@");
	}
	// alert(chk_value.length == 0 ? '你还没有选择任何内容！' : chk_value);
}

function openInsertpro() {
	var data = $('#productform').serialize();
	if (data != null) {
		$("#productform :input").val('');

	}

}

function openEdit() {

}